package fileIO;

import java.util.ArrayList;


import org.jdom.Element;

import storage.Field;

import data.FilePath;

public class FieldInfoWriter {

	public static void write(String fileName, ArrayList<Field> inputList, String titleRowNumber){
		Element root = new Element("FieldColumnMapping");
		
		for(int i=0;i<inputList.size();i++){
			Element field = new Element("Field");
			Element name = new Element("Name");
			Element column = new Element("Column");
			Element type = new Element("Type");
			Element origin = new Element("Origin");
			Element exception = new Element("Exception");
			
			name.addContent(inputList.get(i).name);
			column.addContent(Integer.toString(inputList.get(i).column));
			type.addContent(inputList.get(i).type);
			System.out.println("at saving: " + Boolean.toString(inputList.get(i).origin));
			origin.addContent(Boolean.toString(inputList.get(i).origin));
			exception.addContent(Boolean.toString(inputList.get(i).exception));
			
			field.addContent(name);
			field.addContent(column);
			field.addContent(type);
			field.addContent(origin);
			field.addContent(exception);
			
			root.addContent(field);
		}
		Element title = new Element("Title");
		try{
			//If not int, then don't add title row data
			Integer.parseInt(titleRowNumber);
			title.addContent(titleRowNumber);
		}catch(NumberFormatException e){
			title.addContent("-1");
		}

		
		root.addContent(title);
		if(!fileName.contains(".map"))
		{
			fileName += ".map";
		}
		System.out.println(FilePath.FIELD_COLUMN_MAPPING_DIRECTORY + "//"+ fileName);
		XMLWriter.writeDocument(root, FilePath.FIELD_COLUMN_MAPPING_DIRECTORY + "//"+ fileName);
	}
}
